package com.example.demo.controller;

// HelloController:  接收请求，返回响应

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.config.Result;
import com.example.demo.entity.Article;
import com.example.demo.entity.User;
import com.example.demo.service.ArticleService;
import com.example.demo.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@CrossOrigin
@RestController
@RequestMapping("/article")
@Tag(name = "文章管理")
public class ArticleController {

    @Autowired
    ArticleService articleService;

    @Operation(summary = "根据条件查询用户（查询用户）")
    @GetMapping("/articlePage")
    public Result articlePage(@RequestParam(defaultValue ="1") Integer pageNum,
                              @RequestParam(defaultValue ="10") Integer pageSize,
                              @RequestParam(defaultValue ="") String title,
                              @RequestParam(defaultValue ="") String content,
                              @RequestParam(defaultValue ="") String createTime)
    {

        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Article::getTitle, title)
                .like(Article::getContent,content)
                .like(Article::getCreateTime,createTime)
                .orderByDesc(Article::getCreateTime);

        Page<Article> page = articleService.page(new Page<Article>(pageNum, pageSize), wrapper);
        return Result.ok(page);
    }

    /**
     * 游客不能查看已经下架的
     * @param pageNum
     * @param pageSize
     * @param title
     * @param content
     * @param createTime
     * @return
     */
    @GetMapping("/articlePage1")
    public Result articlePage1(@RequestParam(defaultValue ="1") Integer pageNum,
                              @RequestParam(defaultValue ="10") Integer pageSize,
                              @RequestParam(defaultValue ="") String title,
                              @RequestParam(defaultValue ="") String content,
                              @RequestParam(defaultValue ="") String createTime)
    {

        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Article::getTitle, title)
                .like(Article::getContent,content)
                .like(Article::getCreateTime,createTime)
                .eq(Article::getEnabled,0)
                .orderByDesc(Article::getCreateTime);

        Page<Article> page = articleService.page(new Page<Article>(pageNum, pageSize), wrapper);
        return Result.ok(page);
    }

    @PostMapping("/save")
    public Result saveUser(@RequestBody Article article){
        article.setCreateTime(LocalDateTime.now());
        article.setEnabled(0);
        boolean save = articleService.save(article);
        return Result.ok("发表文章成功！");
    }

    @PostMapping("/update")
    public Result update(@RequestBody Article article){
        UpdateWrapper<Article> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda()
                .set(Article::getEnabled,article.getEnabled())
                .eq(Article::getId,article.getId());
        boolean save = articleService.update(updateWrapper);
        return Result.ok("文章状态已修改！");
    }

    @PostMapping("/edit")
    public Result edit(@RequestBody Article article){
        UpdateWrapper<Article> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda()
                .set(Article::getTitle,article.getTitle())
                .set(Article::getContent,article.getContent())
                .eq(Article::getId,article.getId());
        boolean save = articleService.update(updateWrapper);
        return Result.ok("修改文章成功");
    }

}
